package com.cmcc.he.cz;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class SearchActivity extends Activity implements OnClickListener,
		TextWatcher {

	private AutoCompleteTextView actvWord;
	private Button btnSelectWord;
	private ContactDbAdapter mDbHelper;
	private long employeeid;

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		Cursor cursor = mDbHelper.searchResult(arg0.toString());
		// startManagingCursor(notesCursor);
		DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,
				cursor, true);
		actvWord.setAdapter(dictionaryAdapter);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);

		mDbHelper = new ContactDbAdapter(this);
		mDbHelper.open();
		btnSelectWord = (Button) findViewById(R.id.btnSelectWord);
		actvWord = (AutoCompleteTextView) findViewById(R.id.actvWord);
		btnSelectWord.setOnClickListener(this);
		actvWord.addTextChangedListener(this);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent i = new Intent(this, DetailsActivity.class);
		i.putExtra("pressname", employeeid);
		startActivity(i);
	}

	public class DictionaryAdapter extends CursorAdapter {
		private LayoutInflater layoutInflater;

		@Override
		public CharSequence convertToString(Cursor cursor) {
			employeeid = cursor.getLong(cursor.getColumnIndex("_id"));
			return cursor == null ? "" : cursor.getString(cursor
					.getColumnIndex("mobile"));
		}

		private void setView(View view, Cursor cursor) {
			TextView tvWordItem = (TextView) view;
			tvWordItem.setText(cursor.getString(cursor.getColumnIndex("name")));
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			setView(view, cursor);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = layoutInflater.inflate(R.layout.word_list_item, null);
			setView(view, cursor);
			return view;
		}

		public DictionaryAdapter(Context context, Cursor c, boolean autoRequery) {
			super(context, c, autoRequery);
			layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
	}

}
